/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pio.action;

import DBMS.DataBaseManager;
import clases.*;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpSession;

/**
 *
 * @author charles
 */
public class Entrar extends org.apache.struts.action.Action {

    /*
     * forward cedula="success" path=""
     */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     *
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Registro registro = (Registro) form;
        int validacion = 0;
        registro.setMensaje("");
        DataBaseManager dbms = DataBaseManager.getInstance();
        validacion = dbms.validar(registro);

        if (validacion < 6) {
            HttpSession s = request.getSession();
            registro.setRol(validacion);
            registro.setMensaje("");
            s.setAttribute("cedula", registro.getCedula());
            s.setAttribute("rol", registro.getRol());
            switch (validacion) {
                case 0:
                    cargarDatosAdmin(dbms, request);
                    return mapping.findForward("administrador");
                case 1:
                    return mapping.findForward("profesor");
                case 2:
                    return mapping.findForward("participante");
                case 3:
                    return mapping.findForward("preinscrito");
                case 4:
                    request.setAttribute("aceptacion", "-1");
                    Usuario u = new Usuario();
                    u.setCedula(registro.getCedula());
                    u = (Usuario) (dbms.consultarUsuario(u)).get(0);
                    Periodo periodo = new Periodo();
                    periodo = (Periodo) (dbms.consultarPeriodo(periodo)).get(0);
                    request.setAttribute("Usuario", u);
                    request.setAttribute("Periodo", periodo);
                    return mapping.findForward("aspirante");
                case 5:
                    Coordinador c = new Coordinador();
                    c.setCedula((String) registro.getCedula());
                    List l = dbms.consultarCoordinador(c);
                    cargarDatosCoordinador(dbms, request);
                    if (l.isEmpty()) {
                        registro.setMensaje("Error obteniendo institucion asociada.");
                        return mapping.findForward("failure");
                    } else {
                        c = (Coordinador) l.get(0);
                        //s.setAttribute("institucionId_coordinador", c.getInstitucionId()); //Atributo necesario para evitar tantas consultas

                        return mapping.findForward("coordinador");
                    }
                default:
                    registro.setMensaje("Cedula i/o Clave incorrectas");
                    return mapping.findForward(FAILURE);
            }
        } else {
            registro.setMensaje("Cedula i/o Clave incorrectas");
            return mapping.findForward(FAILURE);
        }
    }

    private void cargarDatosAdmin(DataBaseManager dbms, HttpServletRequest request) {
        int cnl = buscarContactoNoLeido(dbms.consultarContactoCoordinador(new ContactoCoordinador()));
        if (cnl != 0) {
            request.setAttribute("hayContactoCoordinador", cnl);
        }
        cnl = dbms.consultarContactos("false").size();
        if (cnl != 0) {
            request.setAttribute("haySolicitud", cnl);
        }
    }

    private int buscarContactoNoLeido(ArrayList<ContactoCoordinador> consultarContactoCoordinador) {
        int num = 0;
        for (ContactoCoordinador cc : consultarContactoCoordinador) {
//            if (!cc.getLeido()){
//                num+=1;
//            }
        }
        return num;
    }

    private void cargarDatosCoordinador(DataBaseManager dbms, HttpServletRequest request) {
        int cnl = buscarNoticiaNoLeido(dbms.listarNoticias());
        if (cnl != 0) {
            request.setAttribute("hayNoticia", cnl);
        }
    }

    private int buscarNoticiaNoLeido(ArrayList<Noticia> listarNoticias) {
        int num = 0;
        for (Noticia cc : listarNoticias) {
//            if (!cc.getLeido()){
//                num+=1;
//            }
        }
        return num;
    }
}
